#ifndef _IM_SERVER_MESSAGE_SERVICE_HANDLER_
#define _IM_SERVER_MESSAGE_SERVICE_HANDLER_

#include "MessageService_types.h"
#include "MessageService.h"

#include "base/logging.h"
#include "message_id_creator.h"
#include "redis_executor/simple_redis_locator.h"
#include "redis_executor/redis_executor.h"
#include "melon/service/service_arch.h"
#include "melon/service/base_service_impl.h"

namespace imserver {

class MessageServiceHandler : public MessageServiceIf, public melon::service::BaseServiceImpl {
public:
  MessageServiceHandler() /*: redisExecutor(new FixedLocator("127.0.0.1:6379"))*/ {
  }

  virtual void CreateMessage(std::map<int64_t, int64_t>& ret_, const ImMessage& message);
  virtual void GetUnReadMessageInfo(std::string& ret_, int64_t uid);
  virtual void GetP2PHistoryMessage(std::string& ret_, int64_t uid, int64_t another_uid, int64_t min_msg_id, int32_t limit);
  virtual void GetP2PNewMessage(std::string& ret_, int64_t uid, int64_t another_uid, int64_t max_msg_id, int32_t limit);
  virtual void GetGroupHistoryMessage(std::string& ret_, int64_t uid, int64_t group_id, int64_t min_msg_id, int32_t limit);
  virtual void GetGroupNewMessage(std::string& ret_, int64_t uid, int64_t group_id, int64_t max_msg_id, int32_t limit);
  virtual void SetMessageReceiveStatus(int64_t uid, int64_t msg_id, int64_t dialogue_id);
  virtual void SetMessageReadStatus(int64_t uid, int64_t msg_id, int64_t dialogue_id);

private:
  int64_t insertMsgDB(ImMessage& message);
  bool insertRedis(const ImMessage& message);
  void insertContactList(int32_t msg_type, int64_t msg_receiver, int64_t msg_sender,
      int64_t msg_group_id, int64_t msg_id);

  void getUnreadDialogueMessageInfo(int64_t uid, std::map<int64_t, std::pair<int32_t, int64_t> >& res);
  void getUnreadGroupMessageInfo(int64_t uid, std::map<int64_t, std::pair<int32_t, int64_t> >& res);

  //RedisExecutor redisExecutor;
};

}

#endif // _IM_SERVER_MESSAGE_SERVICE_HANDLER_
